package com.samp.solr.file2solr2;

import org.apache.commons.lang3.StringUtils;
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.common.SolrInputDocument;

import java.io.BufferedReader;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;


public class deleteErrorWordsInSolr {

    private static final String SOLR_FIELD_ID = "id";
    private static final String SOLR_FIELD_CONTENT = "content";
    private static final String SOLR_FIELD_INTENTION = "intention";
    private static final String SOLR_FIELD_CONTENT_FULL_MATCH = "contentFullMatch";

    private static String BASE_SOLR_URL = "";

    public static void deleteExists(String rawPath) throws Exception{
        Path filePath = Paths.get(rawPath);
        BufferedReader bufferReader = Files.newBufferedReader(filePath, StandardCharsets.UTF_8);
        String line;
        try {
            SolrClient solrCliect =new HttpSolrClient.Builder(BASE_SOLR_URL).build();
            List<String> totalList = new ArrayList<>();
            List<String> deleteList = new ArrayList<>();
            while( (line = bufferReader.readLine()) != null ){
                SolrInputDocument document = new SolrInputDocument();
                String[] words = line.split("\\|");
                if( words.length == 2){
                    deleteList.add("\""+words[0]+"\"");
                }else {
                    System.out.println("error line:"+line);
                    continue;
                }
                if( deleteList.size() >= 50 ){
                    totalList.add("contentFullMatch:"+StringUtils.join(deleteList, " OR contentFullMatch:"));
                    deleteList.clear();
                }
            }
            if( deleteList.size() >= 0 ){
                totalList.add("contentFullMatch:"+StringUtils.join(deleteList, " OR contentFullMatch:"));
                deleteList.clear();
            }
            for( String s : totalList ){
                solrCliect.deleteByQuery(s);
                System.out.println("delete one ,commit;");
                solrCliect.commit();
            }
            System.out.println("last commit.");
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            bufferReader.close();
        }
    }


    public static void main(String[] args) {
//        BASE_SOLR_URL = "http://localhost:8080/solr/callcenter";
        BASE_SOLR_URL = "http://localhost:8080/solr/gcc-omcp";
        long start = System.currentTimeMillis();
        String rawPath = "D:\\gcc-omcp\\new\\nlp\\data_to_delete.txt";
        try {
            deleteExists(rawPath);
        }catch (Exception e){
            e.printStackTrace();
        }
        System.out.println("总计耗时："+(System.currentTimeMillis()-start)/(1000.0*60.0)+" 分钟");
    }
}
